var express = require('express');
var bodyParser = require("body-parser");
var AppInfoManger = require('../manager/AppInfoManager');
var logger = new require("../log/Logger")("LogAPIRouter");
var LogAPIRouter = express.Router();

LogAPIRouter.use(bodyParser.json());
LogAPIRouter.use(bodyParser.urlencoded({extended: true }));

LogAPIRouter.route('/getAppInfo').get(function(req, res, next) {
    logger.info("new requet get /logApi/getAppInfo");
    var appId = req.query.appId;
    AppInfoManger.getAppInfoByAppId(appId).then(function(appInfo) {
        res.end(JSON.stringify({
            code: 0,
            message: "",
            data: appInfo
        }));
    }).catch(function(err){
        logger.error(err)
        res.end(JSON.stringify({
            code: -1,
            message: "没有获取到数据"
        }));
    });
});

LogAPIRouter.route('/getAppList').get(function(req, res, next) {
    logger.info("new requet get /logApi/getAppList");
    AppInfoManger.getAppInfoList().then(function(appInfoList) {
        res.end(JSON.stringify({
            code: 0,
            message: "",
            data: appInfoList
        }));
    }, function() {
        res.end(JSON.stringify({
            code: -1,
            message: ""
        }));
    }).catch(function(err){
        logger.error(err)
    });
});

LogAPIRouter.route('/getModuleList').get(function(req, res, next) {
    logger.info("new requet get /logApi/getModuleList");

});

LogAPIRouter.route('/getLogList').get(function(req, res, next) {
    logger.info("new requet get /logApi/getLogList");

});

LogAPIRouter.route('/addAppInfo').post(function(req, res, next) {
    logger.info("new requet get /logApi/addAppInfo");
    var appName = req.body.appName;
    var appId = req.body.appId;
    if(appName != null && appName != "" && appId != null && appId != ""){
        AppInfoManger.addNewAppInfo(appName, appId).then(function() {
            res.end(JSON.stringify({
                code: 0,
                message: "add success"
            }));
        }, function() {
            res.end(JSON.stringify({
                code: -1,
                message: "failed"
            }));
        }).catch(function(err){
            logger.error(err)
        });
    }else{
        res.end(JSON.stringify({
            code: -2,
            message: "failed"
        }));
    }
});

module.exports = LogAPIRouter;
